//
//  MainNavigationController.m
//  TradeUnion
//  Supervisor
//
//  Created by 小宇 on 2017/5/10.
//  Copyright © 2017年 北京怀教网络科技股份有限公司. All rights reserved.
//

#import "MainNavigationController.h"
#import "SVProgressHUD.h"
@interface MainNavigationController ()<UINavigationControllerDelegate>
@property(nonatomic,strong) id popDelegate;
@end

@implementation MainNavigationController
- (void)viewDidLoad{
    [super viewDidLoad];
    self.popDelegate = self.interactivePopGestureRecognizer.delegate;
    // 导航栏字体颜色
    NSDictionary * dic=@{NSForegroundColorAttributeName:[UIColor whiteColor]};
    self.navigationBar.titleTextAttributes=dic;
    self.delegate = self;
}

//这里可以封装成一个分类
- (UIBarButtonItem *)barButtonItemWithImage:(NSString *)imageName highImage:(NSString *)highImageName target:(id)target action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.bounds = CGRectMake(0, 0, 9, 16);
    button.imageView.contentMode = UIViewContentModeScaleToFill;
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:highImageName] forState:UIControlStateHighlighted];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    return  [[UIBarButtonItem alloc] initWithCustomView:button];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
     self.interactivePopGestureRecognizer.delegate = nil;
    if (self.viewControllers.count >= 1) {
        viewController.hidesBottomBarWhenPushed = YES;
        if ([SVProgressHUD isVisible]) {
            [SVProgressHUD dismiss];
        }
        UIBarButtonItem *popToPreButton = [self barButtonItemWithImage:@"NavBack" highImage:nil target:self action:@selector(popToPre)];
        viewController.navigationItem.leftBarButtonItem = popToPreButton;
    }
    [super pushViewController:viewController animated:animated];
    
}

- (void)popToPre
{
    if ([SVProgressHUD isVisible]) {
        [SVProgressHUD dismiss];
    }
    [self popViewControllerAnimated:YES];
}


#pragma mark --------navigation delegate
//该方法可以解决popRootViewController时tabbar的bug
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    //删除系统自带的tabBarButton
    for (UIView *tabBar in self.tabBarController.tabBar.subviews) {
        if ([tabBar isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabBar removeFromSuperview];
        }
    }
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.childViewControllers.count == 1) {
        self.interactivePopGestureRecognizer.delegate = self.popDelegate;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
